#pragma once

#ifndef CAMERA_H
#define CAMERA_H

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <vector>

enum Camera_Movement
{
    FORWARD,
    BACKWARD,
    LEFT,
    RIGHT
};

const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 5.0f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;

// An abstract camera class that processes input and calculates the corresponding Euler Angles, Vectors and Matrices for use in OpenGL
class Camera
{
public:
    // camera Attributes
    glm::vec3 BasePosition;
    glm::vec3 BaseFront;
    glm::vec3 BaseUp;

    glm::vec3 SurroundPosition;

    glm::vec3 Position;
    glm::vec3 Front;
    glm::vec3 Up;
    glm::vec3 Right;
    glm::vec3 WorldUp;
    // euler Angles
    float Yaw;
    float BaseYaw;
    float Pitch;
    float BasePitch;
    // camera options
    float MovementSpeed;
    float MouseSensitivity;
    float Zoom;
    float MaxZoom;

    // constructor with vectors
    Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM), MaxZoom(ZOOM)
    {
        Position = position;
        BasePosition = position;
        WorldUp = up;
        BaseFront = Front;
        BaseUp = up;
        Right = glm::normalize(glm::cross(Front, up));
        Yaw = yaw;
        Pitch = pitch;
        BaseYaw = yaw;
        BasePitch = pitch;
        updateCameraVectors();
    }
    // constructor with scalar values
    Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM), MaxZoom(ZOOM)
    {
        Position = glm::vec3(posX, posY, posZ);
        BasePosition = Position;
        WorldUp = glm::vec3(upX, upY, upZ);
        BaseFront = Front;
        BaseUp = WorldUp;
        Right = glm::normalize(glm::cross(Front, WorldUp));
        Yaw = yaw;
        Pitch = pitch;
        BaseYaw = Yaw;
        BasePitch = Pitch;
        updateCameraVectors();
    }

    void Reset()
    {
        Position = BasePosition;
        Front = BaseFront;
        Up = BaseUp;
        Right = glm::normalize(glm::cross(Front, Up));
        Yaw = BaseYaw;
        Pitch = BasePitch;
    }

    // returns the view matrix calculated using Euler Angles and the LookAt Matrix
    glm::mat4 GetViewMatrix()
    {
        return glm::lookAt(Position, Position + Front, Up);
    }

    // processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
    void ProcessKeyboard(Camera_Movement direction, float deltaTime)
    {
        float velocity = MovementSpeed * deltaTime;
        if (direction == FORWARD)
            Position += Front * velocity;
        if (direction == BACKWARD)
            Position -= Front * velocity;
        if (direction == LEFT)
            Position -= Right * velocity;
        if (direction == RIGHT)
            Position += Right * velocity;
    }

    // processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
    void ProcessMouseScroll(float yoffset)
    {
        Zoom -= (float)yoffset;
        if (Zoom < 1.0f)
            Zoom = 1.0f;
        if (Zoom > MaxZoom)
            Zoom = MaxZoom;
    }

    // rotate camera by mouse input
    void RotateByMouse(float xoffset, float yoffset)
    {
        xoffset *= MouseSensitivity;
        yoffset *= MouseSensitivity;

        Yaw += xoffset;
        Pitch += yoffset;

        // make sure that when pitch is out of bounds, screen doesn't get flipped
        if (Pitch > 89.0f)
            Pitch = 89.0f;
        if (Pitch < -89.0f)
            Pitch = -89.0f;

        // update Front, Right and Up Vectors using the updated Euler angles
        updateCameraVectors();
    }

    void UpdateSurroundPos(){
        SurroundPosition = glm::normalize(glm::cross(Front, glm::cross(Front, -Position))) * (glm::length(glm::cross(glm::normalize(Front), glm::normalize(-Position))) * glm::length(Position));
    }

    // rotate camera by mouse input
    void RotateByMouseSurround(float xoffset, float yoffset)
    {

        float length = glm::length(Position - SurroundPosition);

        xoffset *= MouseSensitivity;
        yoffset *= MouseSensitivity;

        Yaw += xoffset;
        Pitch += yoffset;

        // make sure that when pitch is out of bounds, screen doesn't get flipped
        if (Pitch > 89.0f)
            Pitch = 89.0f;
        if (Pitch < -89.0f)
            Pitch = -89.0f;

        // update Front, Right and Up Vectors using the updated Euler angles
        updateCameraVectors();

        Position = -Front * length + SurroundPosition;
    }

private:
    // calculates the front vector from the Camera's (updated) Euler Angles
    void updateCameraVectors()
    {
        // calculate the new Front vector
        glm::vec3 front;
        front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
        front.y = sin(glm::radians(Pitch));
        front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
        Front = glm::normalize(front);
        // also re-calculate the Right and Up vector
        Right = glm::normalize(glm::cross(Front, WorldUp)); // normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
        Up = glm::normalize(glm::cross(Right, Front));
    }

    void updateCameraYawPitchByFront()
    {
        Yaw = glm::degrees(glm::atan(Front.x, Front.z));
        Pitch = glm::degrees(glm::asin(Front.y));
    }
};

#endif